import {getAppsubject,setLastApp,getVipSoft} from './getData'
import {compareObj ,setSelectedApp,setCookie} from './mUtils'
/**
 *  根据AppENames获取版本信息
 *  输入参数：科目相关信息
 *  创建信息：赵梦琦(2018-12-12)
 */
export const upSubject = (json) =>{
	let promise = new Promise(function(resolve, reject){
		let _appID = json.AppID;
		let verData;
		let guid = localStorage.getItem('guid');
		let _vn,_vName;
		if(json.AppVn){
		    _vn = json.AppVn;
            _vName = json.AppVnName;
        }

		getAppsubject(_appID,guid,1).then(res=>{
			if(res.status === 200){
				let dataJson ={};
				var jsonData = res.data;
				if(!jsonData){
				    return;
                }
				for(let key in jsonData){
		            if(key!=='ar'){
		            	dataJson['has'+ key] = 1
		            }
		        }
				if(!jsonData.ar || jsonData.ar.length===0){
				    return;
                }
            	verData = jsonData.ar.sort(compareObj("vnorder"));
            	let maxVerData = verData[verData.length - 1];
                if (maxVerData.vn === 2 && verData.length>1) {
                    maxVerData = verData[verData.length - 2];
                }
                if(!_vn){
                    _vn = maxVerData.vn;
                    _vName = maxVerData.vname;
                }
                setSelectedApp(maxVerData.CName, maxVerData.AppEName, maxVerData.AppID,_vn,_vName);
                var operJson = {};
                operJson.VerNO = maxVerData.vn; //版本编号
                operJson.VerJSON = dataJson; //版本功能配置json
                operJson.VerName = maxVerData.vname;
                setCookie('AppVerOperJSON', JSON.stringify(operJson));
//	                this.getAppENameJump()
			}
			resolve(res)
		})
	})
	return promise
}

/**
 *  根据AppENames获取版本信息
 *  输入参数：科目相关信息
 *  创建信息：赵梦琦(2018-12-12)
 */
export const getVipAppData = (isNew) =>{
	if(!localStorage.getItem('guid') || !localStorage.getItem('userID')){
		let promise = new Promise(function(resolve, reject){
			resolve(false)
		})
		return promise
	}
	let promise = new Promise(function(resolve, reject){
		let guid = localStorage.getItem('guid');
		if(!localStorage.getItem('vipAppData') || isNew){
			getVipSoft(guid).then(res=>{
				if(res.status === 200){
					localStorage.setItem('vipAppData',JSON.stringify(res.data.VipApp) )
				}
				resolve(JSON.stringify(res.data.VipApp))
			})
		}else{
			resolve(localStorage.getItem('vipAppData'))
		}
	})
	return promise
}
